﻿using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Xml;

namespace T.FAS.Runtime.DataAccess.SQLDom
{
    /// <summary>
    /// SQL语句类
    /// </summary>
    /// <remarks>SQL语句</remarks>
    [Serializable]
    public abstract class SqlStatement : SqlElement
    { 
        /// <summary>
        /// 构造函数
        /// </summary>
        public SqlStatement()
            : base()
        {
            PrimaryKeys = new SqlPrimaryKey();
            SqlBuildingInfo = new SqlBuildingInfo();
        }

        /// <summary>
        /// 表名称
        /// </summary>
        public string TableName { get; set; }

        private string tableCode;

        /// <summary>
        /// 表编号
        /// </summary>
        public string TableCode
        {
            get
            {
                if (string.IsNullOrEmpty(tableCode))
                    return TableName;

                return tableCode;
            }
            set
            {
                tableCode = value;
            }
        }

        /// <summary>
        /// 主键列表
        /// </summary>
        public SqlPrimaryKey PrimaryKeys { get; set; }

        private SqlBuildingInfo sqlBuildingInfo;

        /// <summary>
        /// SQL语句构造中用到的中间变量
        /// </summary>
        public SqlBuildingInfo SqlBuildingInfo
        {
            get
            {
                return sqlBuildingInfo;
            }
            set
            {
                sqlBuildingInfo = value;
            }
        }

        /// <summary>
        /// 通用中间对象ID
        /// </summary>
        public string CommonObjectID { get; set; }

        /// <summary>
        /// 请求的唯一标识
        /// </summary>
        public string RequestTokenID { get; set; }

        /// <summary>
        /// 通用中间对象对应的元数据版本
        /// </summary>
        public string CommonObjectVersion { get; set; }

        /// <summary>
        /// 数据库配置连接串
        /// </summary>
        public string DatabaseConfigKey 
        { 
            get
            {
                return SqlBuildingInfo.DataSource;
            }
            set
            {
                /**********低代码改动:提供DQ修改ConfigKey的功能**********/
                SqlBuildingInfo.DataSource = value;
            }
            
        }

        /// <summary>
        /// 元数据标识
        /// </summary>
        public string MetaDataCode
        {
            get
            {
                return SqlBuildingInfo.MetaDataCode;
            }
            set
            {
                /**********低代码改动:提供DQ修改ConfigKey的功能**********/
                SqlBuildingInfo.MetaDataCode = value;
            }
        }

        /// <summary>
        /// 节点对象ID
        /// </summary>
        private string nodeID;

        /// <summary>
        /// 节点对象ID
        /// </summary>
        public string NodeID
        {
            get
            {
                if (string.IsNullOrWhiteSpace(nodeID))
                    return this.SqlBuildingInfo.CurrentNode.ID;
                else
                    return nodeID;
            }
            set
            {
                nodeID = value;
            }
        }

        /// <summary>
        /// 节点对象的版本
        /// </summary>
        public string NodeVersion { get; set; }
       
        /// <summary>
        /// 克隆
        /// </summary>
        /// <returns>SQL语句类副本</returns>
        public override object Clone()
        {
            SqlStatement sqlStatement = base.Clone() as SqlStatement;
            if (PrimaryKeys != null)
                sqlStatement.PrimaryKeys = PrimaryKeys.Clone() as SqlPrimaryKey;

            sqlStatement.SqlBuildingInfo = SqlBuildingInfo.Clone() as SqlBuildingInfo;

            return sqlStatement;
        }
    }
}
